package cn.flyingocean.spider.impl.com.douban.book.com.douban.book;

import cn.flyingocean.spider.impl.com.douban.book.FileHandlerSpider;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 一般是把任务分解给多个爬虫来分步骤实施，而这个例子是把一个任务分解到一个爬虫的不同方法中，所以称为 Self—Join（自联接，自加入）
 */
public class DoubanSpider_Self_Join extends FileHandlerSpider {
    private String url;
    private int cnt = 0;


    public Object doWork(Document document) {
        if (document==null) return null;

        Element infoElement = document.getElementById("info");
        List<TextNode> r = infoElement.textNodes();
        List<String> infos = new ArrayList<String>();

        for (int i=0;i<r.size();i++){
            String t = r.get(i).toString().replace("&nbsp;","").replace((char) 12288, ' ').trim();
            if (t.length()!=0){
                infos.add(t);
            }
        }

        Map<String,String> rMap = new HashMap<String, String>();

        Elements pls = infoElement.getElementsByClass("pl");

        for (int i=0,k=0;i<pls.size();i++){
            String key = pls.get(i).text();
            String value = "";
            if (key.indexOf(":")==-1){
                Elements siblings = pls.get(i).siblingElements();
                for (int j=0;j<siblings.size();j++){
                    value += siblings.get(j).text();
                    if (j!=siblings.size()-1){
                        value += "/";
                    }
                }
            }else if (pls.get(i).nextElementSibling().tagName().equals("a")){
                value = pls.get(i).nextElementSibling().text();
            }else {
                value = infos.get(k);
                k++;
            }
            key = key.replace(":","");
            rMap.put(key,value);
        }

        // 获取图书封面图片链接
        Element a = document.getElementsByClass("nbg").first();
        String[] strings1 = a.attr("href").split("/");
        rMap.put("图片名", strings1[strings1.length-1]);
        // 获取书名
        String bookName = document.getElementsByTag("h1").first().text();
        rMap.put("书名",bookName);
        return rMap;
    }

    public Object doWork(String url) {
        return null;
    }

    /**
     * 若没有该书的详情，就返回null
     * @return
     */
    public Object doWork() {
        try {
            Document document = Jsoup.connect(this.getUrl()).timeout(99999).get();
            if (document.getElementsByTag("h1")==null){
                return null;
            }
            return document;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getUrl() {
        return url;
    }

    public DoubanSpider_Self_Join setUrl(String url){
        this.url = url;
        return this;
    }

    @Override
    public void onCompleted(Object r) {
        if (r==null||r instanceof Document) return;
        System.out.println(this.getUrl());
        BufferedWriter writer = getWriter();
        try {
            writer.write(r.toString());
            writer.newLine();
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected String getOutputPath() {
        return "C:\\Users\\胡伟\\Desktop\\data.txt";
    }

}
